package org.issaclu.springbootmybatis.user.service;

import org.issaclu.springbootmybatis.user.bo.UserDetailBo;
import org.issaclu.springbootmybatis.user.dto.UserInfo;
import org.issaclu.springbootmybatis.user.dto.UserInfoDetail;
import org.issaclu.springbootmybatis.user.mapper.*;
import org.issaclu.springbootmybatis.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final UserInfoMapper userInfoMapper;

    private final UserInfoQueryMapper userInfoQueryMapper;
    private final UserInfoBasicQueryMapper userInfoBasicQueryMapper;
    private final UserInfoReuseDemoMapper userInfoReuseDemoMapper;
    private final UserInfoReuseDemo2Mapper userInfoReuseDemo2Mapper;

    public UserInfoService(UserInfoMapper userInfoMapper, UserInfoQueryMapper userInfoQueryMapper,
                           UserInfoBasicQueryMapper userInfoBasicQueryMapper,
                           UserInfoReuseDemoMapper userInfoReuseDemoMapper,
                           UserInfoReuseDemo2Mapper userInfoReuseDemo2Mapper) {
        this.userInfoMapper = userInfoMapper;
        this.userInfoQueryMapper = userInfoQueryMapper;
        this.userInfoBasicQueryMapper = userInfoBasicQueryMapper;
        this.userInfoReuseDemoMapper = userInfoReuseDemoMapper;
        this.userInfoReuseDemo2Mapper = userInfoReuseDemo2Mapper;
    }

    public UserInfo insert(UserInfo info) {
        info.setId(Utils.uuid());
        int result = this.userInfoMapper.insertUser(info);
        logger.info("插入数据条数：{}", result);
        return info;
    }

    public List<UserInfo> query(UserDetailBo param) {
        return this.userInfoQueryMapper.queryWithDynamicWhere(param);
    }

    public UserInfo getById(String id) {
        return this.userInfoBasicQueryMapper.getById(id);
    }

    public List<UserInfoDetail> getInfoWithDetailByName(String name) {
        return this.userInfoBasicQueryMapper.getUserWithDetailByName(name);
    }

    public UserInfo getUserInfoWithDetail(String id) {
        return this.userInfoBasicQueryMapper.getUserInfoWithDetail(id);
    }

    public UserInfo getUserInfoWithDetail2(String id) {
        return this.userInfoBasicQueryMapper.getUserInfoWithDetail2(id);
    }
    public UserInfo getUser(String id) {
        UserInfo result = this.userInfoBasicQueryMapper.getUser(id);
        logger.info("关联查询急加载，关联sql已经查询： {}", result);
        return result;
    }
    public UserInfo getUser2(String id) {
        UserInfo result = this.userInfoBasicQueryMapper.getUser2(id);
        logger.info("关联查询懒加载，关联sql没有查询，下一行代码因为访问的懒加载的属性才触发查询，所以sql会打印在这个日志之后");
        result.getDetail();
        return result;
    }

    public UserInfo getUserWithHouse(String userId) {
        return this.userInfoBasicQueryMapper.getUserWithHouse(userId);
    }
    public UserInfo getUserWithHouse2(String userId) {
        return this.userInfoBasicQueryMapper.getUserWithHouse2(userId);
    }
    public List<UserInfo> queryAll(){
        return userInfoReuseDemoMapper.queryAll();
    }

    public List<UserInfo> queryAll2(){
        return userInfoReuseDemoMapper.queryAll2();
    }
    public List<UserInfo> queryAll3(){
        return userInfoReuseDemoMapper.queryAll3();
    }

    public List<UserInfo> query2(UserDetailBo param) {
        return userInfoReuseDemo2Mapper.query(param);
    }
}
